package com.example.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ContactDetailActivity extends AppCompatActivity {

    private TextView tvContactName, tvContactPhone, tvContactEmail, tvContactAddress;
    private Button btnEditContact, btnDeleteContact;
    private ContactDAO contactDAO;
    private Contact contact;
    private int userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_detail);

        // 获取联系人ID和用户ID
        int contactId = getIntent().getIntExtra("contactId", -1);
        userId = getIntent().getIntExtra("userId", -1);
        if (contactId == -1 || userId == -1) {
            finish();
            return;
        }

        // 初始化视图组件
        tvContactName = findViewById(R.id.tv_contact_name);
        tvContactPhone = findViewById(R.id.tv_contact_phone);
        tvContactEmail = findViewById(R.id.tv_contact_email);
        tvContactAddress = findViewById(R.id.tv_contact_address);
        btnEditContact = findViewById(R.id.btn_edit_contact);
        btnDeleteContact = findViewById(R.id.btn_delete_contact);

        // 初始化 DAO
        contactDAO = new ContactDAO(this);

        // 加载联系人信息
        loadContact(contactId);

        // 设置编辑按钮点击事件
        btnEditContact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editContact();
            }
        });

        // 设置删除按钮点击事件
        btnDeleteContact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDeleteConfirmationDialog();
            }
        });
    }

    private void loadContact(int contactId) {
        contact = contactDAO.getContactById(contactId);
        if (contact == null) {
            Toast.makeText(this, "联系人不存在", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        tvContactName.setText(contact.getName());
        tvContactPhone.setText("电话: " + contact.getPhone());
        tvContactEmail.setText("邮箱: " + contact.getEmail());
        tvContactAddress.setText("地址: " + contact.getAddress());
    }

    private void editContact() {
        Intent intent = new Intent(this, EditContactActivity.class);
        intent.putExtra("contactId", contact.getId());
        intent.putExtra("userId", userId);
        startActivity(intent);
    }

    private void showDeleteConfirmationDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确认删除");
        builder.setMessage("确定要删除此联系人吗？");
        builder.setPositiveButton("删除", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                deleteContact();
            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();
    }

    private void deleteContact() {
        int result = contactDAO.deleteContact(contact.getId());
        if (result > 0) {
            Toast.makeText(this, "联系人已删除", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
        }
    }
}
